import { computed } from 'vue'
import {isUndefined,isNull} from '../../utils'
import type { ComputedRef,Ref } from 'vue'
// 空对象或空数组都不认为是空值，防止替换了用户的数据
const isEmpty = (val: unknown) => {
	return isUndefined(val)||isNull(val);
};

export const useDefaultProps = <T>(props: Record<string,any>,defaultProps:Ref<Record<string,any>>): ComputedRef<Required<T>> => {
  return computed(() => {
	  let _props:Record<string,any> = {}
	  // 获取props的所有key
	  let keys = new Set(Object.keys(defaultProps.value).concat(Object.keys(props)))
	  Array.from(keys).forEach(key => {
		  if(isEmpty(props[key])){
			  _props[key] = defaultProps.value[key]
		  }else{
			   _props[key] = props[key]
		  }
	  })
	  return _props as Required<T>
  })
}
